package com.mlnx.device.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mlnx.common.form.PageForm;
import com.mlnx.device.dao.mysql.SugarDeviceMapper;
import com.mlnx.device.entity.Device;
import com.mlnx.device.entity.SugarDevice;
import com.mlnx.device.pojo.dto.SugarDeviceDto;
import com.mlnx.device.pojo.form.SugarDeviceFilterForm;
import com.mlnx.device.pojo.form.SugarDeviceForm;
import com.mlnx.device.service.DeviceService;
import com.mlnx.device.service.SugarDeviceService;
import com.mlnx.device.session.manager.MutilDeviceSessionManager;
import com.mlnx.device.util.SortUtil;
import com.mlnx.service.base.service.BaseService;
import com.mlnx.service.common.enums.ResponseEnum;
import com.mlnx.service.common.exception.MutilDeviceServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class SugarDeviceServiceIml extends BaseService<SugarDeviceMapper, SugarDevice> implements SugarDeviceService {
    @Autowired
    @Override
    public void setBaseMapper(SugarDeviceMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private MutilDeviceSessionManager sessionManager;


    @Override
    @Transactional
    public void addSugarDevice(SugarDeviceForm sugarDeviceForm) {
        Device device = new Device();
        device.setNumeration(sugarDeviceForm.getNumeration());
        device.setValidTime(sugarDeviceForm.getValidTime());
        device.setDeviceTypeId(sugarDeviceForm.getDeviceType());
        device.setPatientId(sugarDeviceForm.getPatientId() == null ? 0 : sugarDeviceForm.getPatientId());

        deviceService.save(device);

        try {
            SugarDevice spoDevice = new SugarDevice();
            spoDevice.setId(device.getId());
            spoDevice.setMac(sugarDeviceForm.getMac());

            save(spoDevice);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new MutilDeviceServiceException(ResponseEnum.ServerError);
        }
    }

    @Override
    public Page<SugarDeviceDto> list(PageForm pageForm, SugarDeviceFilterForm sugarDeviceFilterForm) {
        Page<SugarDeviceDto> sugarDeviceDtoPage = baseMapper.selectDeviceDtoPage(new Page<SugarDeviceDto>(pageForm.getCurrent(),
                pageForm.getSize()), sugarDeviceFilterForm, SortUtil.parseSorter(sugarDeviceFilterForm == null ? null : sugarDeviceFilterForm.getSorter()));

        List<String> deviceIds = sugarDeviceDtoPage.getRecords().stream().map(SugarDeviceDto::getNumeration).collect(Collectors.toList());

        Collection<String> onlineDeviceIds = sessionManager.getOnlineDeviceIds(deviceIds);

        if (sugarDeviceDtoPage.getRecords() != null && sugarDeviceDtoPage.getRecords().size() > 0 && onlineDeviceIds != null && onlineDeviceIds.size() > 0) {
            for (SugarDeviceDto spoDeviceDto : sugarDeviceDtoPage.getRecords()) {
                for (String onlineDeviceId : onlineDeviceIds) {
                    if (spoDeviceDto.getNumeration().equals(onlineDeviceId)) {
                        spoDeviceDto.setState(1);
                        break;
                    }
                }
            }
        }

        return sugarDeviceDtoPage;
    }

    @Override
    public List<SugarDeviceDto> listOnlines(SugarDeviceFilterForm spoDeviceFilterForm) {
        Page<SugarDeviceDto> spoDeviceDtoPage = baseMapper.selectDeviceDtoPage(new Page<SugarDeviceDto>(1l,
                1000l), spoDeviceFilterForm, SortUtil.parseSorter(spoDeviceFilterForm.getSorter()));

        List<String> deviceIds = spoDeviceDtoPage.getRecords().stream().map(SugarDeviceDto::getNumeration).collect(Collectors.toList());

        Collection<String> onlineDeviceIds = sessionManager.getOnlineDeviceIds(deviceIds);

        if (spoDeviceDtoPage.getRecords() != null && spoDeviceDtoPage.getRecords().size() > 0 && onlineDeviceIds != null && onlineDeviceIds.size() > 0) {
            for (SugarDeviceDto spoDeviceDto : spoDeviceDtoPage.getRecords()) {
                for (String onlineDeviceId : onlineDeviceIds) {
                    if (spoDeviceDto.getNumeration().equals(onlineDeviceId)) {
                        spoDeviceDto.setState(1);
                        onlineDeviceIds.remove(onlineDeviceId);
                        break;
                    }
                }
            }
        }

        return spoDeviceDtoPage.getRecords();
    }

    @Override
    @Transactional
    public void updateSugarDevice(Integer id, SugarDeviceForm spoDeviceForm) {
        Device device = new Device();
        device.setId(id);
        device.setNumeration(spoDeviceForm.getNumeration());
        if (spoDeviceForm.getPatientId() != null) {
            device.setPatientId(spoDeviceForm.getPatientId());
            deviceService.bindDevice(spoDeviceForm.getPatientId(), spoDeviceForm.getNumeration());
        }

        deviceService.updateById(device);

        try {
            SugarDevice spoDevice = new SugarDevice();
            spoDevice.setId(id);
            spoDevice.setMac(spoDeviceForm.getMac());

            updateById(spoDevice);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MutilDeviceServiceException(ResponseEnum.ServerError);
        }
    }
}
